package com.dyc.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.annotation.WebServlet;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
//@WebFilter(urlPatterns = "/*")//拦截所有请求
@Slf4j
public class DemoFilter implements Filter {
    //初始化.服务器启动时执行
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        Filter.super.init(filterConfig);
    }

    //拦截到请求后,执行,会执行多次
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("拦截到请求:{}",servletRequest.getServletContext().getContextPath());
        //放行
        filterChain.doFilter(servletRequest,servletResponse);

    }

    //销毁.服务器关闭时执行
    @Override
    public void destroy() {
        Filter.super.destroy();
    }
}
